<?php
/**
 * @package scuttle
 * @file /class/handler/Bookmarks.class.php
 **/

if (!defined('XOOPS_ROOT_PATH')) exit();

class ScuttleBookmarksObject extends XoopsSimpleObject
{
	function ScuttleBookmarksObject()
	{
		$this->initVar('bId'          , XOBJ_DTYPE_INT, '', false);
		$this->initVar('uId'          , XOBJ_DTYPE_INT, '0', false);
		$this->initVar('bIp'          , XOBJ_DTYPE_STRING, '', false, 40);
		$this->initVar('bStatus'      , XOBJ_DTYPE_INT, '0', true);
		$this->initVar('bDatetime'    , XOBJ_DTYPE_STRING, '0000-00-00 00:00:00', false, 19);
		$this->initVar('bModified'    , XOBJ_DTYPE_STRING, date('Y-m-d H:i:s', time()) , false, 19);
		$this->initVar('bTitle'       , XOBJ_DTYPE_STRING, '', false, 255);
		$this->initVar('bAddress'     , XOBJ_DTYPE_TEXT, '', false);
		$this->initVar('bDescription' , XOBJ_DTYPE_STRING, '', false, 255);
		$this->initVar('image'        , XOBJ_DTYPE_STRING, '', true, 255);
	}
}

class ScuttleBookmarksHandler extends XoopsObjectGenericHandler
{

	var $mTable = 'scuttle_bookmarks';
	var $mPrimary = 'bId';
	var $mClass = 'ScuttleBookmarksObject';

	// --------------------------------------------------------
	// Search Action
	// --------------------------------------------------------
	function searchBookmarks($queryArray, $userId = 0, $status = 0)
	{
		if (!is_array($queryArray)) {
			return false;
		}

		$retArray = array();
		$sql = "SELECT bId, bTitle, bAddress, bDescription, bStatus, image FROM ". $this->mTable." WHERE bStatus < 2 ";

		if (is_array($queryArray) && $count = count($queryArray)) {
			// Full text search
			$sql .= " AND ((  bTitle       LIKE '%$queryArray[0]%' OR 
						bAddress     LIKE '%$queryArray[0]%' OR 
						bDescription LIKE '%$queryArray[0]%')";
						for($i = 1;$i < $count;$i++) {
							$sql .= " AND ";
							$sql .= "(  bTitle       LIKE '%$queryArray[$i]%' OR 
									bAddress     LIKE '%$queryArray[$i]%' OR 
									bDescription LIKE '%$queryArray[$i]%')";
						}
			$sql .= ") ";
		} 
		$sql .= " ORDER BY bModified DESC";
		$result = $this->db->query($sql);
		while (list($bId, $bTitle, $bAddress, $bDescription, $bStatus, $image) = $this->db->fetchRow($result)) {
			array_push($retArray, array(
				'bId'  => $bId,
				'bTitle' => $bTitle,
				'bAddress'  => $bAddress,
				'bDescription' => $bDescription,
				'bStatus' => $bStatus,
				'image' => $image,
			));
		}
		return $retArray;
	}

	// --------------------------------------------------------
	// String feiled Update
	// use for inline edit (Ajax)
	// inplace.php
	// --------------------------------------------------------
	function updateValue($bId=0, $updateValue)
	{
		if ($bId < 1) {
			return false;
		} else {
			$bId = intval($bId);
		}
		if ($updateValue == '') {
			return false;
		} else {
			$updateValue = htmlSpecialChars($updateValue, ENT_QUOTES ) ;
		}
		$sql = sprintf("UPDATE `%s` SET `bDescription` = '%s' WHERE `bId`=%u LIMIT 1 ", $this->mTable , $updateValue, $bId ) ;

		return $this->db->query($sql);
	}

	// --------------------------------------------------------
	// delete bookmark
	// use for inline delete (Ajax)
	// ajaxDelete.php
	// --------------------------------------------------------
	function deleteBookmark($bId = NULL) {
		$sql = sprintf("DELETE FROM `%s` WHERE `%s`='%s'", $this->mTable, $this->mPrimary, $bId );
		return $this->db->queryF($sql);
	}


}
?>